﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using NTTDATA.BEENChina.ResultCertification.Config;

namespace NTTDATA.BEENChina.ResultCertification.Checker.Date
{
    //[Serializable]
    [VerfityAttribute(Id = "VerfityRq0219", Name = "两行日期间隔检证", CategoryType = EnumCategoryType.日期, Model = true)]
    public class VerfityRq0219 : VerfityOneResult
    {
        private int _monthSpan = 2;
        /// <summary>
        ///         
        /// </summary>
        [Category("期间间隔,单位:月"), Description("输入间隔值")]
        public int MonthSpan
        {
            get { return _monthSpan; }
            set { _monthSpan = value; }
        }

        public override bool CheckResult(List<List<string>> result, string entryCommon)
        {
            bool bresult = false;

            //找到第0列
            if (result != null && result.Count>0 )
            {
                List<string> firstColumns = result.Select(t => t[0]).ToList();

                firstColumns = base.CompleteDate(firstColumns);

 
               // int emptyCol = EmptyColumnCount(firstColumns);
                
 
                for (int i = 0; i < firstColumns.Count; i++)
                {
                    string date = firstColumns[i];


                    if (EmptyResultCount(date) == 0)
                        firstColumns[i] = CompleteDate(  date);
                }


                for (int i = 0; i < firstColumns.Count - 1; i++)
                {
                    string preDateStr = firstColumns[i];
                    string nextDateStr = firstColumns[i + 1];

                    DateTime? fistDate = GetEntryDate(preDateStr);
                    DateTime? secondDate = GetEntryDate(nextDateStr);

                    if (secondDate != null && fistDate != null)
                    {
                        if (monthSpan(fistDate.Value, secondDate.Value) > _monthSpan)
                        {
                            bresult = true;
                            break;
                        }
                    }
                }
            }


        

            if (bresult)
            {
                System.Console.WriteLine("VerfityRQ0219");
            }
            
            return bresult;
        }

        private int monthSpan(DateTime fistDate, DateTime secondDate)
        {
            return (secondDate.Year - fistDate.Year) * 12 + (secondDate.Month - fistDate.Month);
        }

        private DateTime? GetEntryDate(string entryResult)
        {
            if (string.IsNullOrEmpty(entryResult))
            {
                return null;
            }

            var splitEntryResult = new Split.SplitEntryResult(ChildImageObject.CategorySetting.DataFormat, entryResult);
            DateTime? entryDate = splitEntryResult.GetDateTime();

            return entryDate;
        }

    
    }
}
